home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 2) (1997).iso / misc / coding / pump_src / convert.c < prev    next >
C/C++ Source or Header  |  1996-04-24  |  2KB  |  88 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. //
  7. // Raw to RLE (run length encoded) converter
  8. //
  9.  
  10. typedef unsigned char byte;
  11.  
  12. int Encode(byte *pix, byte *rle) {
  13.     int nbytes = 0;
  14.     byte curbyte = *pix;
  15.     int howmany = 0;
  16.     byte *dest = rle;
  17.     byte *src = pix;
  18.  
  19.     for (;;) {
  20.         if (pix - src == 320*200) {
  21.             *dest++ = howmany;
  22.             *dest++ = curbyte;
  23.             *dest++ = 0;
  24.             *dest++ = 0;
  25.             return dest - rle;
  26.         }
  27.         if (howmany == 255) {
  28.             *dest++ = howmany;
  29.             *dest++ = curbyte;
  30.             howmany = 0;
  31.             curbyte = *pix;
  32.             continue;
  33.         }
  34.  
  35.         if (*pix != curbyte) {
  36.             *dest++ = howmany;
  37.             *dest++ = curbyte;
  38.             howmany = 0;
  39.             curbyte = *pix;
  40.             continue;
  41.         } else {
  42.             howmany++;
  43.             pix++;
  44.         }
  45.     }
  46. }
  47.  
  48.  
  49. main(int argc, char *argv[]) {
  50.     FILE *fdin, *fdout;
  51.     byte *buf;
  52.     byte *buf2;
  53.     int size;
  54.  
  55.     if (argc != 3) {
  56.         printf("CONVERT <pixfile> <outfile>\n");
  57.         return 1;
  58.     }
  59.     fdin = fopen(argv[1], "rb");
  60.     if (!fdin) {
  61.         printf("Cannot openread\n");
  62.         return 1;
  63.     }
  64.     fdout = fopen(argv[2], "wb");
  65.     if (!fdout) {
  66.         printf("Cannot create\n");
  67.         return 1;
  68.     }
  69.     buf = malloc(320*200);
  70.     buf2 = malloc(100000);
  71.     if (!buf || !buf2) {
  72.         printf("outamem\n");
  73.         return 1;
  74.     }
  75.     if (fread(buf, 320, 200, fdin) != 200) {
  76.         printf("Cannot read\n");
  77.         return 1;
  78.     }
  79.     fclose(fdin);
  80.  
  81.     size = Encode(buf, buf2);
  82.     fwrite(buf2, 1, size, fdout);
  83.     fclose(fdout);
  84.     printf("ok");
  85.     return 0;
  86. }
  87.  
  88.